const myAxios = (obj) => {
  return new Promise((resolve, reject) => {
    const{url,method,data,params}=obj
    //1
    const xhr=new XMLHttpRequest()
    //2
    if (method.toUpperCase() === 'GET') {
      xhr.open(method, params?url+'?'+fn(params):url)
      //3
      xhr.send()
    }
    else {
      xhr.open(method, url)
      xhr.setRequestHeader('Content-Type', Object.prototype.toString.call(data)==='[object Object]'?'application/json':'application/x-www-form-urlencoded')
      xhr.send(Object.prototype.toString.call(data)==='[object Object]'?JSON.stringify(data):data)
    }

    //4
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
        if (xhr.status >= 200 && xhr.status < 300) {
          resolve(JSON.parse(xhr.responseText))
        }
        else {
          reject('傻逼，你错了')
        }
      }
    }
  })
}
const fn = (obj) => {
  const arr=[]
  for (let k in obj) {
    arr.push(k+'='+obj[k]) 
  }
  return arr.join('&')
}
